package teacher;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import applicationDAO.InstituteListDAO;

import com.project.trac.R;

import core.modules.DatabaseConnector;

public class MySchedules extends InstituteListDAO {


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.generic_list_b);
		setTitle("My Schedules");
		
		new AsynchronousTask().execute();
		
		//TODO: when clicked, will go to class manager
		//ViewGroup root = (ViewGroup) getLayoutInflater().inflate(R.layout.teacher_subjects_view, null);
		//TextView tv = (TextView) root.findViewById(R.id.text_view_instructor);
		//tv.setText("Code : ");
		//System.out.println(tv.getText().toString());

		//TODO: on item click, delete or edit
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> av, View view, int position, long id) {
				System.out.println("click " + av.getItemAtPosition(position));
			}
		});

	}



	private class AsynchronousTask extends AsyncTask<String, String, String> {

		HashMap<String, String> m;
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();

		ListAdapter listAdapter;

		String KEY_CAT = "CatalogNumber";
		String KEY_SECTION = "k2";
		String KEY_DAYS = "k3";
		String KEY_TIME = "k4";
		String KEY_ROOM = "k5";
		String KEY_REGISTRATION = "k6";
		String KEY_STUDENTS = "k7";

		ResultSet rs;
		
		ProgressDialog progressDialog;

		protected void onPreExecute() {
			super.onPreExecute();
			
			progressDialog = new ProgressDialog(MySchedules.this);
			progressDialog.setTitle("Please Wait");
			progressDialog.setMessage("Loading list...");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(true);
			progressDialog.show();
		}

		protected String doInBackground(String... args) {

			try {
				rs = DatabaseConnector.getStatement().executeQuery(
						"SELECT schedule.id, curriculum.CatalogNumber, schedule.Days, schedule.Time, "
						+ "schedule.Section, schedule.Room, schedule.Code,"
							/*-- start subquery -- */
							+ " (SELECT COUNT(gradesheet.uid) "
							+ " FROM gradesheet"
							+ " WHERE gradesheet.sid = schedule.id) AS '# of Students' "
							/*-- end subquery -- */
						+ " FROM schedule "
						+ " INNER JOIN users ON users.id = schedule.Creator "
						+ " INNER JOIN curriculum ON curriculum.id = schedule.CatalogNumber "
						+ " WHERE users.id =" + getUserID());

				while (rs.next()) {

					m = new HashMap<String, String>();
					m.put(KEY_CAT, rs.getString("CatalogNumber"));
					m.put(KEY_STUDENTS, rs.getString("# of Students"));
					m.put(KEY_SECTION, rs.getString("Section"));
					m.put(KEY_DAYS, rs.getString("Days"));
					m.put(KEY_TIME, rs.getString("Time"));
					m.put(KEY_ROOM, rs.getString("Room"));
					m.put(KEY_REGISTRATION, rs.getString("Code"));

					arrayList.add(m);

				}
				
				rs.close();

			} catch (SQLException e) {
				Log(getClass(), "doInBackground", e);
			}

			return null;
		}
		

		protected void onPostExecute(String postExecute) {

			progressDialog.dismiss();
			
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					listAdapter = new SimpleAdapter(MySchedules.this, arrayList,
							R.layout.teacher_subjects_view, new String[] { KEY_CAT, KEY_STUDENTS,
							KEY_SECTION, KEY_DAYS, KEY_TIME, KEY_ROOM,
							KEY_REGISTRATION }, new int[] {
							R.id.r_key_cat_new, R.id.number_students, R.id.section,
							R.id.days, R.id.time,
							R.id.room, R.id.code});

					setListAdapter(listAdapter);
				}
			});
		}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.teacher_options, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch(item.getItemId()) {
		case R.id.NEW_SCHEDULE:
			this.startActivity(new Intent(this, NewSchedule.class));
			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}

}